package common;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class CommonUtil {
	public static String getDateTimeStr() {
		String time = getDate().toString();
		return time.substring(0, 19);
	}

	public static String getDateStr() {
		String time = getDate().toString();
		return time.substring(0, 10);
	}
/**
 * 返回yyyy-MM-dd格式时间
 * @param sDate
 * @return
 */
	public static Timestamp dateParser(String sDate) {
		return timeParser(sDate, "yyyy-MM-dd");
	}
/**
 * 返回yyyy-MM-dd hh:mm:ss格式时间
 * @param sDate
 * @return
 */
	public static Timestamp dateTimeParser(String sDate) {
		return timeParser(sDate, "yyyy-MM-dd hh:mm:ss");
	}

	public static Timestamp timeParser(String sDate, String format) {
		SimpleDateFormat sdf = new SimpleDateFormat(format);
		DateFormat dateFormatterChina = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);// 格式化输出
		TimeZone timeZoneChina = TimeZone.getTimeZone("GMT+8");// 获取时区
		dateFormatterChina.setTimeZone(timeZoneChina);// 设置系统时区
		Date date = null;
		try {
			date = sdf.parse(dateFormatterChina.format(new Date(sDate)));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return new Timestamp(date.getTime());
	}

	public static Timestamp getDate() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		DateFormat dateFormatterChina = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);// 格式化输出
		TimeZone timeZoneChina = TimeZone.getTimeZone("GMT+8");// 获取时区
		dateFormatterChina.setTimeZone(timeZoneChina);// 设置系统时区
		Date date = null;
		try {
			date = sdf.parse(dateFormatterChina.format(new Date()));
		} catch (ParseException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}

		return new Timestamp(date.getTime());
	}

	public static Timestamp getDateTime() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
		DateFormat dateFormatterChina = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);// 格式化输出
		TimeZone timeZoneChina = TimeZone.getTimeZone("GMT+8");// 获取时区
		dateFormatterChina.setTimeZone(timeZoneChina);// 设置系统时区
		Date date = null;
		try {
			date = sdf.parse(dateFormatterChina.format(new Date()));
		} catch (ParseException e) {
			// TODO 自动生成 catch 块
			e.printStackTrace();
		}

		return new Timestamp(date.getTime());
	}

	public static Timestamp addYear(Timestamp val, int num) {
		return calendarAdd(val, 1, num);
	}

	public static Timestamp addMonth(Timestamp val, int num) {
		return calendarAdd(val, 2, num);
	}

	public static Timestamp addDay(Timestamp val, int num) {
		return calendarAdd(val, 5, num);
	}

	private static Timestamp calendarAdd(Timestamp val, int field, int num) {
		if (val == null) {
			return val;
		}
		GregorianCalendar calendar = new GregorianCalendar();
		calendar.setTime(val);
		calendar.add(field, num);
		return new Timestamp(calendar.getTime().getTime());
	}

	public static double add(double v1, double v2) {
		BigDecimal b1 = new BigDecimal(Double.toString(v1));
		BigDecimal b2 = new BigDecimal(Double.toString(v2));
		return b1.add(b2).doubleValue();
	}

	public static double sub(double v1, double v2) {
		BigDecimal b1 = new BigDecimal(Double.toString(v1));
		BigDecimal b2 = new BigDecimal(Double.toString(v2));
		return b1.subtract(b2).doubleValue();
	}

	public static double mul(double v1, double v2) {
		BigDecimal b1 = new BigDecimal(Double.toString(v1));
		BigDecimal b2 = new BigDecimal(Double.toString(v2));
		return b1.multiply(b2).doubleValue();
	}

	public static double div(double v1, double v2) {
		return div(v1, v2, 10);
	}

	public static double div(double v1, double v2, int scale) {
		if (scale < 0) {
			throw new IllegalArgumentException("The scale must be a positive integer or zero");
		}
		BigDecimal b1 = new BigDecimal(Double.toString(v1));
		BigDecimal b2 = new BigDecimal(Double.toString(v2));
		return b1.divide(b2, scale, 4).doubleValue();
	}

	public static double round(double v, int scale) {
		if (scale < 0) {
			throw new IllegalArgumentException("The scale must be a positive integer or zero");
		}
		BigDecimal b = new BigDecimal(Double.toString(v));
		BigDecimal one = new BigDecimal("1");
		return b.divide(one, scale, 4).doubleValue();
	}

	public static String ltrim(String val) {
		if (val == null) {
			return val;
		}

		int len = val.length();
		int start = 0;
		while ((start < len) && (val.charAt(start) <= ' ')) {
			start++;
		}
		return start > 0 ? val.substring(start, len) : val;
	}

	public static String rtrim(String val) {
		if (val == null) {
			return val;
		}

		int len = val.length();
		int end = len;
		while ((end > 0) && (val.charAt(end - 1) <= ' ')) {
			end--;
		}
		return end < len ? val.substring(0, end) : val;
	}

	public static String charFormat(String charValue, int len) {
		if (charValue == null) {
			return null;
		}
		charValue = rtrim(charValue);
		StringBuffer buffer = new StringBuffer(charValue);
		int charLen = charValue.length();
		for (int i = 0; i < len - charLen; i++) {
			buffer.append(" ");
		}
		return buffer.toString();
	}

	public static String charZeroFormat(String charValue, int len) {
		if (charValue == null) {
			return null;
		}
		StringBuffer buffer = new StringBuffer("");
		int charLen = charValue.length();
		for (int i = 0; i < len - charLen; i++) {
			buffer.append("0");
		}
		buffer.append(charValue);
		return buffer.toString();
	}
}
